<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Gregwar\Captcha\CaptchaBuilder;
use Session;

class UserController extends Controller
{

    // public function personcenter(Request $request){

    //     $id = $request->session()->get('id');
    //     // dd($id);
    //     if(empty($id)){
    //         return redirect('/home/login/login');
    //     }
    //     $data = DB::table('users')->where('id',$id)->first();
    //      // dd($data);
    //     $alldata = DB::table('order')
    //         ->where('uid',$id)
    //         ->select('status')
    //         ->get();
    //     if(empty($alldata)){
    //     	$str = '0';
    //     }else{
    //     	foreach ($alldata as $k => $v) {
    //         	$alldata[$k] = $v->status;
    //     	}
    //     // dd($alldata);
	   //      foreach ($alldata as $key => $value) {
	   //          $str = implode(',',$alldata);
	   //      }
    //     }
        
    //     // dd($str);
    //     $dat = DB::table('collect')
    //         ->where('uid',$id)
    //         ->get();
    //     // dd($dat);

    // 	return view('home.user.personcenter',['data'=>$data,'crumb'=>'个人中心','str'=>$str,'dat'=>$dat]);
    // }


    //===============================地址===============================
	//地址列表
    public function address(Request $request){
        $id = $request->session()->get('id');
        if(empty($id)){
            return redirect('/home/login/login');
        }
        $data = DB::table('addr')
            ->where('uid',$id)
            ->get();
        // dd($data);
    	return view('home.user.address',['crumb'=>'收货地址','data'=>$data]);
    }
    //添加地址
    public function doaddress(Request $request){
        // return 111;
        $id = $request->session()->get('id');
        if(empty($id)){
            return redirect('/home/login/login');
        }
        $data = $request->except('_token');
        $data['uid'] = $id;
        // dd($data);
        $res = DB::table('addr')->insert($data);
        if($res){
            return redirect('/home/user/address');
        }else{
            return back();
        }
    }
    //修改地址
    public function editaddress(Request $request,$id){
        $idd = $request->session()->get('id');
        if(empty($idd)){
            return redirect('/home/login/login');
        }
        $data = DB::table('addr')
            ->where('uid',$idd)
            ->get();

        $alldata = DB::table('addr')
            ->where('id',$id)
            ->first();
        // dd($alldata);
        return view('home..user.editaddress',['crumb'=>'收货地址','data'=>$data,'alldata'=>$alldata]);
    }

    //执行修改操作
    public function saveaddress(Request $request){
        // return 111;
        $data = $request->except('_token','id');
        // dd($data);
        $res = DB::table('addr')->where('id',$request->id)->update($data);
        if($res){
            // return '修改成功';
            return redirect('/home/user/address');
        }else{
            // return '修改失败';
            return back();
        }

    }
    //删除地址
    public function deladdress($id){
        // return 111;
        $res = DB::table('addr')->where('id',$id)->delete();
        if($res){
            // return '删除成功';
            return redirect('/home/user/address');
        }else{
            // return '删除失败';
            return back();
        }

    }


    //收藏列表
    public function mylike(Request $request){
        $id = $request->session()->get('id');
        if(empty($id)){
            return redirect('/home/login/login');
        }
        $data = DB::table('collect')
            ->where('uid',$id)
            ->join('goods','collect.gid','=','goods.id')
            ->select('collect.*','goods.name','goods.img','goods.price')
            ->get();
        // dd($data);
        return view('home.user.mylike',['crumb'=>'喜欢的商品','data'=>$data]);
    }
    //删除收藏
    public function delmylike($id){
        // return 111;
        $res = DB::table('collect')->where('id',$id)->delete();
        if($res){
            // return '删除成功';
            return redirect('/home/user/mylike');
        }else{
            return back();
        }

    }


}
